"""
将ios的资源文件改名为android的
"""
iosPath = "D:/ProjectKm/jiangemian/Ui/见个面/见个面/见个面_切图"
androidPath = "./aa"

import os;
import re
from pypinyin import pinyin,  Style

xxPath = os.path.join(androidPath, "drawable-xxhdpi")
xPath = os.path.join(androidPath, "drawable-xhdpi")
lPath = os.path.join(androidPath, "drawable-hdpi")


def getDirName(param):
    """

    :param param: 原文件的目录
    :return:
    """
    lastDir = os.path.split(param)[1]
    name = getPingying(lastDir)
    print("getDirName:", param, name)
    return name


def getPingying(lastDir):
    name=""
    t = pinyin(lastDir, style=Style.NORMAL)
    for item in t:
        name += (item[0] + "_")
    return name


def doCopyFile1(split):
    """

    :param split:  原文件 绝对文件名
    :return:
    """
    print("doCopyFile1:", split)
    fileName = split[1]
    aPath = lPath
    if (fileName.find("@3x") > 0):
        aPath = xxPath
    elif (fileName.find("@2x") > 0):
        aPath = xPath
    splitext = os.path.splitext(fileName) #文件名 分成 文件名和扩展名
    print("splitext:", splitext)
    indexAt = fileName.find("@")
    aDir = getDirName(split[0])  # 相对一级目录名
    descFileName = splitext[0][0:indexAt if indexAt > 0 else 0]  # 文件名
    # descFileName = aDir + descFileName
    descFileName = getPingying(descFileName)
    descFileName = re.sub(r"[/ :、.。，？！,：‘@]*", "", descFileName); #文件名中去掉特殊字符
    descExt = splitext[1]  # 扩展名
    finalFullName = descFileName + descExt  # 最终完整的目标名称
    finalFullName = finalFullName.lower()
    print("finalFullName:", finalFullName)
    doCopyFile2(os.path.join(split[0], split[1]), os.path.join(aPath, finalFullName))


pass


def doCopyFile2(src, target):
    """

    :param src: 原文件
    :param target:  目标文件
    :return:
    """
    print("doCopyFile2:", src, target)
    split_target_ = os.path.split(target)[0]
    if not os.path.exists(split_target_):
        os.makedirs(split_target_)
    _file = open(target, "wb")
    _file.write(open(src, "rb").read())
    _file.close()
    pass


def doCopyDir(path):
    """复制文件"""
    for file in os.listdir(path):
        join = os.path.join(path, file)
        if os.path.isdir(join):
            doCopyDir(join)
        else:
            print(join)
            split = os.path.split(join);
            doCopyFile1(split)

    pass


def doRemove(path):
    """清空文件"""
    for file in os.listdir(path):
        join = os.path.join(path, file)
        if os.path.isdir(join):
            doCopyDir(join)
        else:
            os.remove(join)

    pass


if __name__ == "__main__":
    """清除文件"""
    if (not os.path.exists(androidPath)):
        os.makedirs(androidPath)
    else:
        doRemove(androidPath)
    """开始复制"""
    doCopyDir(iosPath)

